package com.golddaymicromall.activity.eleaccounts;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.golddaymicromall.activity.R;
import com.golddaymicromall.application.MyApplication;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.rxbus.RxBus;
import com.golddaymicromall.util.ActivityController;
import com.golddaymicromall.util.ButtonUtils;
import com.golddaymicromall.util.MD5;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.math.BigDecimal;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import rx.android.schedulers.AndroidSchedulers;

public class ZhuangZhangZhuangChuMoneyActivity extends BaseActivity {
    private TextView Tv_zhuanruzhanghuname;
    private TextView mTv_zhuangzhang1;
    private String customerno1;
    private String customerid;
    private String Et_zhuangzhang;
    private EditText mEt_zhuanruzhanghubianhao;
    private EditText mEt_zhichujin_e;
    private Button mbtnZhuanZhangZhuanchu;
    private boolean flag = true;
    private String walletid;
    private LinearLayout mZhuanBack;
    private String mDeptFlag;
    private String Customerno;
    private String Et_zhuangzhang1;
    private String Balance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        walletid = getIntent().getStringExtra("walletid");
        Balance = getIntent().getStringExtra("Balance");
        Customerno = SPUtils.get(getApplicationContext(), "Customerno", "").toString();
        customerid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        mDeptFlag = SPUtils.get(getApplicationContext(), "deptFlag", "").toString();
        setContentView(R.layout.activity_zhuang_zhang_zhuang_chu_money);
        initView();
    }

    private void initView() {
        Tv_zhuanruzhanghuname = (TextView) findViewById(R.id.Tv_zhuanruzhanghuname);
        customerno1 = SPUtils.get(getApplicationContext(), "Customerno", "").toString();
        mTv_zhuangzhang1 = (TextView) findViewById(R.id.Tv_zhuangzhang1);
        mEt_zhuanruzhanghubianhao = (EditText) findViewById(R.id.Et_zhuanruzhanghubianhao);
        mZhuanBack = (LinearLayout) findViewById(R.id.activity_zhuangzhangzhuangchu_btn_back);
        mTv_zhuangzhang1.setText(customerno1);
        mbtnZhuanZhangZhuanchu = (Button) findViewById(R.id.btnZhuanZhangZhuanchu11);

        mEt_zhichujin_e = (EditText) findViewById(R.id.Et_zhichujin_e);
        mEt_zhichujin_e.setSelection(mEt_zhichujin_e.getText().length());
        mbtnZhuanZhangZhuanchu.setOnClickListener(v -> {
            if (ButtonUtils.isFastDoubleClick()) {
                return;
            }
            String text = (String) Tv_zhuanruzhanghuname.getText();
            Et_zhuangzhang = mEt_zhuanruzhanghubianhao.getText().toString();
            if ("".equals(Et_zhuangzhang)) {
                Toast.makeText(getApplicationContext(), "转入账号不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            if (Et_zhuangzhang.equals(customerno1)) {
                Toast.makeText(getApplicationContext(), "不能给自己转账", Toast.LENGTH_SHORT).show();
                return;
            }
            if (text.equals("\"编号输入不正确！\"")) {
                Toast.makeText(getApplicationContext(), "\"编号输入不正确！", Toast.LENGTH_SHORT).show();
            } else if (text.equals("\"不在同一体系！\"")) {
                Toast.makeText(getApplicationContext(), "不在同一体系！", Toast.LENGTH_SHORT).show();

            } else if (text.equals("") || text == null) {
                Toast.makeText(getApplicationContext(), "编号不不能为空！", Toast.LENGTH_SHORT).show();
            } else {
                attemptzhuanzhangzhuanchumoney();
            }
        });


        mEt_zhuanruzhanghubianhao.setOnFocusChangeListener((v, hasFocus) -> {

            if (hasFocus) {
                // 此处为得到焦点时的处理内容
            } else {
                // 此处为失去焦点时的处理内容
                attemptbiaohaoyanzhengzheng();
            }
        });

        mZhuanBack.setOnClickListener(v -> finish());

    }

    public boolean isMoney(String Num) {
        Pattern p = Pattern
                .compile("^(([1-9]\\d{0,9})|0)(\\.\\d{1,2})?$");
        Matcher m = p.matcher(Num);
        return m.matches();
    }

    private void attemptbiaohaoyanzhengzheng() {

        //获取本地数据

        Et_zhuangzhang = mEt_zhuanruzhanghubianhao.getText().toString().trim();
        if (Et_zhuangzhang.equals(customerno1)) {
            Toast.makeText(getApplicationContext(), "不能给自己转账", Toast.LENGTH_SHORT).show();
        } else {
            Map<String, String> form = Maps.newHashMap();
            form.put("method", "rop.customer.gettixi");
            form.put("appKey", "00001");
            form.put("v", "1.0");
            form.put("format", "json");
            form.put("customerno", Et_zhuangzhang);
            form.put("logid", customerid);
            form.put("flag", mDeptFlag);
            String sing = RopUtils.signString(form, "qwertyuiop");
            form.put("sign", sing);

            OkGo.post(new ServerConfig().SERVER)
                    .params(form)
                    .getCall(StringConvert.create(), RxAdapter.<String>create())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(result -> {
                        if (!"".equals(result)) {
                            if (result.contains("errorToken") || "0".equals(result)) {
                                Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                            } else if ("null".equals(result)) {
                                Toast.makeText(getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                            } else {
                                Tv_zhuanruzhanghuname.setText(result);
                            }
                        } else {
                            Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                        }
                    }, throwable -> {
                        showError();
                        throwable.printStackTrace();
                    });

        }

    }

    private void attemptzhuanzhangzhuanchumoney() {

        //获取本地数据
        Et_zhuangzhang = mEt_zhuanruzhanghubianhao.getText().toString().trim();
        Et_zhuangzhang1 = mEt_zhichujin_e.getText().toString().trim();
        if ("".equals(Et_zhuangzhang)) {
            Toast.makeText(getApplicationContext(), "转入账号不能为空", Toast.LENGTH_SHORT).show();
            return;
        } else if (Et_zhuangzhang.equals(customerno1)) {
            Toast.makeText(getApplicationContext(), "不能给自己转账", Toast.LENGTH_SHORT).show();
            return;
        } else if (!isMoney(Et_zhuangzhang1)) {
            Toast.makeText(getApplicationContext(), "请输入正确的金额格式", Toast.LENGTH_SHORT).show();
        } else if (Double.parseDouble(Et_zhuangzhang1) < 1) {
            Toast.makeText(getApplicationContext(), "转账金额不能小于1元", Toast.LENGTH_SHORT).show();
        } else if (Et_zhuangzhang1.equals("")) {
            Toast.makeText(getApplicationContext(), "转账金额不能为空", Toast.LENGTH_SHORT).show();
        } else if (new BigDecimal(Et_zhuangzhang1).compareTo(new BigDecimal(Balance)) > 0) {
            Toast.makeText(getApplicationContext(), "对不起，您不能超额转账", Toast.LENGTH_SHORT).show();
        } else {
            MD5 md = new MD5();
            String code = md.toMD5(Et_zhuangzhang1);

            Map<String, String> form = Maps.newHashMap();
            form.put("method", "rop.tranfer.get");
            form.put("appKey", "00001");
            form.put("v", "1.0");
            form.put("format", "json");
            form.put("customerid", customerid);
            form.put("walletid", walletid);
            form.put("outmoney", Et_zhuangzhang1);
            form.put("memo", "A:");
            form.put("outmoney2", code);
            form.put("customerNo", Et_zhuangzhang);
            String sing = RopUtils.signString(form, "qwertyuiop");
            form.put("sign", sing);

            OkGo.post(new ServerConfig().SERVER)
                    .params(form)
                    .getCall(StringConvert.create(), RxAdapter.<String>create())
                    .doOnSubscribe(() -> showLoading())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(result -> {
                        dismissLoading();
                        if (!"".equals(result)) {
                            if (result.contains("errorToken")) {
                                Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                            } else if ("0".equals(result)) {
                                Toast.makeText(getApplicationContext(), "连接超时，请重试", Toast.LENGTH_SHORT).show();
                            } else if ("null".equals(result)) {
                                Toast.makeText(getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                            } else {
                                if (result.equals("1")) {
                                    Toast.makeText(getApplicationContext(), "转账成功", Toast.LENGTH_SHORT).show();
//                                    Intent intent = new Intent(ZhuangZhangZhuangChuMoneyActivity.this, DianZiZhangHuActivity.class);
//                                    intent.putExtra("ZhuangZhangZhuangChuMoneyflag", 1);
//                                    startActivity(intent);

                                    RxBus.get().post("rxbus1", new String("1"));
                                    finish();
                                    ActivityController.getInstance().popActivity();
                                    MyApplication.getInstance().attemptRiZhi(Customerno, Customerno + "转给" + Et_zhuangzhang + Et_zhuangzhang1);
                                } else {
                                    Toast.makeText(getApplicationContext(), "转账失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        } else {
                            Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                        }
                    }, throwable -> {
                        dismissLoading();
                        showError();
                        throwable.printStackTrace();
                    });

        }


    }

}
